﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.Models;
using IdentityServer4.Services;
using IdentityServer4.Stores;
using Ids4Review.AuthorizationServer.Models;
using Ids4Review.AuthorizationServer.Services;
using Microsoft.AspNetCore.Mvc;

namespace Ids4Review.AuthorizationServer.Controllers
{
    public class ConsentController : Controller
    {
        private readonly ConsentService _consentService;
        public ConsentController(ConsentService consentService)
        {
            _consentService = consentService;
        }
        public async Task<IActionResult> Index(string returnUrl = null)
        {
            ViewData["returnUrl"] = returnUrl;
            var vm = await _consentService.BuildConsentViewModelAsync(returnUrl);
            return View(vm);
        }
        [HttpPost]
        public async Task<IActionResult> Index(ConsentInputVm inputVm, string returnUrl = null)
        {
            var rt = await _consentService.ProcessGrantAsync(inputVm, returnUrl);
            if (rt.Success)
            {
                return Redirect(rt.ReturnUrl);
            }
            return View(rt.Consent);
        }
    }
}